package com.app.twth.ui.putaway;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.app.twth.app.Constant;
import com.app.twth.base.BaseAndroidViewModel;
import com.app.twth.bean.BaseResponseEntity;
import com.app.twth.bean.LoadingBean;
import com.app.twth.net.JsonCallBack;
import com.app.twth.ui.lpnphoto.LPNPhotoDetails;
import com.app.twth.ui.tally.lpn.TalliedSku;
import com.lzy.okgo.model.Response;

import java.util.List;


/**
 * ****************************************************************
 * 文件名称: LoginViewModel
 * 文件描述:
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class PutawayViewModel extends BaseAndroidViewModel<PutawayDataModel> {

    public MutableLiveData<List<Putatay>> putawayContainerLiveData = new MutableLiveData<>();
    public MutableLiveData<Integer> tallyDetailsLiveData = new MutableLiveData<>();
    public MutableLiveData<PutatayInfo> skuListMutableLiveData = new MutableLiveData<>();


    public PutawayViewModel(@NonNull Application application) {
        super(application);
    }

    public void getPutawayContainer() {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.<List<Putatay>>getPutawayContainer(new JsonCallBack<BaseResponseEntity<List<Putatay>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<Putatay>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<Putatay>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    putawayContainerLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void getPutawayDetail(int position, int id) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.<List<LPNPhotoDetails>>getPutawayDetail(id, new JsonCallBack<BaseResponseEntity<List<PutawayDetails>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<PutawayDetails>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<PutawayDetails>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    List<PutawayDetails> data = baseResponseEntity.data;
                    if (data == null || data.size() == 0) {
                        return;
                    }
                    List<Putatay> tallyDetailList = putawayContainerLiveData.getValue();
                    if (tallyDetailList == null || tallyDetailList.size() == 0 || tallyDetailList.size() < position) {
                        return;
                    }

                    for (int i = 0; i < tallyDetailList.size(); i++) {
                        Putatay tallyDetail = tallyDetailList.get(i);
                        if (position == i) {//设置显示并添加数据
                            tallyDetail.setShow(true);
                            tallyDetail.setContainerDetail(data);
                        }
                    }
                    tallyDetailsLiveData.setValue(position);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void getIbLpnSkuList(String lpn) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.<PutatayInfo>getIbLpnSkuList(lpn, new JsonCallBack<BaseResponseEntity<PutatayInfo>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<PutatayInfo>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<PutatayInfo> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    PutatayInfo data = baseResponseEntity.data;
                    if (data == null) {
                        infoResultMsg = "Nothing to deal with SKU";
                        errorResultCode.setValue(infoResultMsg);
                        return;
                    }
                    List<TalliedSku> skuInfoList = data.getSkuInfoList();

                    if (skuInfoList == null) {
                        infoResultMsg = "Nothing to deal with SKU";
                        errorResultCode.setValue(infoResultMsg);
                        return;
                    }
                    if (skuInfoList.size() == 0) {
                        infoResultMsg = "Nothing to deal with SKU";
                        errorResultCode.setValue(infoResultMsg);
                        return;
                    }
                    data.setIbLpn(lpn);
                    skuListMutableLiveData.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

}
